package com.myx.juc.simple;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 描述:线程实现方式Callable(jdk1.5以后新增)
 * @author: myx
 * @date: 2018/11/23
 * Copyright © 2018-hotpot. All rights reserved.
 */
public class CallableDemo implements Callable<String> {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        List<FutureTask> taskList=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            FutureTask task =new FutureTask(new CallableDemo());
            new Thread(task).start();
            taskList.add(task);
        }
        for (FutureTask task : taskList) {
            //获取返回值
            System.out.println(task.get());
        }
    }
    @Override
    public String call() throws InterruptedException {
        Thread.sleep(RandomUtil.randomInt(10000));
        System.out.println(Thread.currentThread().getName()+ DateUtil.now() +"我执行了");
        return Thread.currentThread().getName()+"我是实现了Callable的线程";
    }
}
